package com.fdd.furns.web;

import com.fdd.furns.entity.Member;
import com.fdd.furns.service.MemberService;
import com.fdd.furns.service.impl.MemberServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/memberServlet")
public class MemberServlet extends BasicServlet {

    private final MemberService memberService = new MemberServiceImpl();

//    @Override
//    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("dopost");
//        String action = req.getParameter("action");
//        //判断，调用不同的方法
//        if ("login".equals(action)) {
//            login(req, resp);
//        } else if ("register".equals(action)) {
//            register(req, resp);
//        }else{
//            resp.getWriter().write("action类型有误");
//        }
//
//    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }


    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("loginServlet被调用");
        
        // 验证码校验
        String code = req.getParameter("code");
        String sessionCode = (String) req.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        req.getSession().removeAttribute("KAPTCHA_SESSION_KEY"); // 防止重复使用
        
        if (sessionCode == null || !sessionCode.equalsIgnoreCase(code)) {
            req.setAttribute("errorMes", "验证码错误");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            return;
        }
        
        String username = req.getParameter("username1");
        String password = req.getParameter("password1");
        Member member = new Member(null, username, password, null);
        if (memberService.login(member) != null) {
            //说明登录成功，跳转页面
//           resp.sendRedirect("/furnishing/views/member/login_ok.jsp");
            //将登录成功的用户放入session域中
            req.getSession().setAttribute("username", username);
            req.getRequestDispatcher("/views/member/login_ok.jsp").forward(req, resp);
        } else {
            //登录失败，给出提示信息，不改变用户名
            System.out.println("登录失败");
            req.setAttribute("errorMes", "用户名或密码错误");
            req.setAttribute("username", username);
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }

    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("doPost...");
        
        // 验证码校验
        String code = req.getParameter("code");
        String sessionCode = (String) req.getSession().getAttribute("KAPTCHA_SESSION_KEY");
        req.getSession().removeAttribute("KAPTCHA_SESSION_KEY"); // 防止重复使用
        
        if (sessionCode == null || !sessionCode.equalsIgnoreCase(code)) {
            req.setAttribute("errorMes", "验证码错误");
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
            return;
        }
        
        //接受用户注册的信息，这里一定要看前端的页面
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        //判断是否存在，存在就不能注册
        if (!memberService.isExistUsername(username)) {
            Member member = new Member(null, username, password, email);
            if (memberService.registerMember(member)) {
                //注册成功
                //将登录成功的用户放入session域中
                req.getSession().setAttribute("username", username);
                req.getRequestDispatcher("/views/member/register_ok.html").forward(req, resp);
            } else {
                //注册失败
                req.getRequestDispatcher("/views/member/register_fail.html").forward(req, resp);
            }
        }else{
            //用户名已经存在，返回登录页面
            req.getRequestDispatcher("/views/member/login.jsp").forward(req, resp);
        }
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());

    }
}
